﻿Imports Microsoft.Win32

Public Class AutoStartManager
    Private appName As String = "MyApp"
    Private appPath As String = Application.ExecutablePath

    ' 设置开机自启动
    Public Sub EnableAutoStart()
        Try
            Dim regKey As RegistryKey = Registry.CurrentUser.OpenSubKey(
                "Software\Microsoft\Windows\CurrentVersion\Run", True)
            regKey.SetValue(appName, """" & appPath & """")
            regKey.Close()
        Catch ex As Exception
            Logger.Log("开机自启动设置失败: " & ex.Message)
        End Try
    End Sub

    ' 取消开机自启动
    Public Sub DisableAutoStart()
        Try
            Dim regKey As RegistryKey = Registry.CurrentUser.OpenSubKey(
                "Software\Microsoft\Windows\CurrentVersion\Run", True)
            regKey.DeleteValue(appName, False)
            regKey.Close()
        Catch ex As Exception
            Logger.Log("取消开机自启动失败: " & ex.Message)
        End Try
    End Sub

    ' 查询是否已设置开机自启动
    Public Function IsAutoStartEnabled() As Boolean
        Try
            Dim regKey As RegistryKey = Registry.CurrentUser.OpenSubKey(
                "Software\Microsoft\Windows\CurrentVersion\Run", False)
            Dim value As Object = regKey.GetValue(appName, Nothing)
            regKey.Close()
            Return value IsNot Nothing
        Catch ex As Exception
            Logger.Log("读取开机自启动状态失败: " & ex.Message)
            Return False
        End Try
    End Function
End Class


